#ifndef TIME_H
#define TIME_H


#include <time.h>
#include <stdio.h>
#include <stdlib.h>
/***
 * 将YYYYMMDD格式的日期转换为time_t(unix时间戳)
 * @param date 日期字符串
 * 
 * 
*/

time_t date_to_time_t(const char *date){
    struct tm tm = {0};
    sscanf(date, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
    tm.tm_year -= 1900;  // Years since 1900
    tm.tm_mon -= 1;  // 0-11
    return mktime(&tm);
}

/***
 * 将time_t(unix时间戳)转换为YYYYMMDD格式的日期
 * @param time 时间戳
 * 
*/
char *time_t_to_date(time_t time){
    struct tm *tm = localtime(&time);
    char *date = (char *)malloc(9);
    strftime(date, 9, "%Y%m%d", tm);
    return date;
}

/***
 * 将YYYYMMDD格式的日期转换为YYYY年MM月DD日格式的日期
 * @param date 日期字符串
 * 
 * 
*/
char *date_to_chinese(const char *date){
    struct tm tm = {0};
    sscanf(date, "%4d%2d%2d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
    tm.tm_year -= 1900;  // Years since 1900
    tm.tm_mon -= 1;  // 0-11
    char *chinese = (char *)malloc(20);
    strftime(chinese, 20, "%Y年%m月%d日", &tm);
    return chinese;
}

/***
 * 将time_t(unix时间戳)转换为YYYY年MM月DD日格式的日期
 * @param time 时间戳
 * 
*/
char *time_t_to_chinese(time_t time){
    struct tm *tm = localtime(&time);
    char *chinese = (char *)malloc(20);
    strftime(chinese, 20, "%Y年%m月%d日", tm);
    return chinese;
}

#endif